<?php


// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------


function unicode_encode($name) {


   $name = iconv('UTF-8', 'UCS-2', $name);
   $len = strlen($name);
   $str='';
   for($i = 0;$i<$len-1;$i=$i+2){
       $c = $name[$i];
       $c2 = $name[$i+1];
       if(ord($c)>0){
           $str.='\\\\\\\\u'.base_convert(ord($c),10,16).str_pad(base_convert(ord($c2),
                   10,16),2,0,STR_PAD_LEFT);
       }else{
           $str.=$c2;
       }
   }
   return $str;
}


// 应用公共文件
function cdnurl($url,$mode=false){
    if(empty($url)){return '';}
    $arr = parse_url($url);
    if(isset($arr['scheme'])){
        if($arr['host']=='houseimg2.jiezhui.top'){
            return $url;
        }
    }
    $new_url = '';
    $new_url .= 'https://housev2.jiezhui.top/';
    if(isset($arr['path'])){
        $new_url .= $arr['path'];
    }
    if(isset($arr['query'])){
        $new_url .= '?'.$arr['query'];
    }
    return $new_url;
}


function cdnurl2($url,$mode=false){
    if(empty($url)){return '';}
    $arr = parse_url($url);
    if($mode){
        if(isset($arr['scheme'])){
            return $url;
        }
    }
    $new_url = '';
    $new_url .= 'http://houseimg2.jiezhui.top/attachment/';
    if(isset($arr['path'])){
        $new_url .= $arr['path'];
    }
    if(isset($arr['query'])){
        $new_url .= '?'.$arr['query'];
    }
    return $new_url;
}


function random($length=6, $type='string', $convert=0){
    $config = array(
        'number'=>'1234567890',
        'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
        'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
        'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
    );

    if(!isset($config[$type])) $type = 'string';
    $string = $config[$type];

    $code = '';
    $strlen = strlen($string) -1;
    for($i = 0; $i < $length; $i++){
        $code .= $string{mt_rand(0, $strlen)};
    }
    if(!empty($convert)){
        $code = ($convert > 0)? strtoupper($code) : strtolower($code);
    }
    return $code;
}



function is_mobile() {
    if(!isset($_SERVER['HTTP_USER_AGENT'])){return false;}


    if (empty($_SERVER['HTTP_USER_AGENT'])) {
        return false;
    } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
        || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false
    ) {
        return true;
    } else {
        return false;
    }
}




function isjson($str)
{
    return is_null(json_decode($str)) ? false : true;
}


function isMobile($mobile = '')
{
    if (preg_match("/^1[3456789]{1}\d{9}$/", $mobile)) {
        return true;
    } else {
        return false;
    }
}


function getSetting($key = '')
{
    $systemSettingModel = new \app\common\model\Setting();
    return $systemSettingModel->getSettingValue($key);
}


function get_date_dir()
{
    $dir = '/' . date('Y') . '/' . date('m') . '/' . date('d');
    return $dir;
}

function get_hash()
{
    $chars   = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()+-';
    $random  = $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)] . $chars[mt_rand(0, 73)];
    $content = uniqid() . $random;
    return sha1($content);
}




function mkdirs($dir, $mode = 0777)
{
    if (is_dir($dir) || mkdir($dir, $mode, true)) return true;
    if (!mkdirs(dirname($dir), $mode)) return false;
    return mkdir($dir, $mode, true);
}


function getRealUrl($url = '')
{
    if (stripos($url, 'http') !== false || stripos($url, 'https') !== false) {
        return $url;
    } else {
        $storage_params = getSetting('image_storage_params');
        if (isset($storage_params['domain']) && $storage_params['domain']) {
            return $storage_params['domain'] . $url;
        }
        if (config('common.image_storage.domain')) {
            return config('common.image_storage.domain') . $url;
        }
        return request()->domain() . $url;
    }
}




/**
 * 递归删除文件
 * @param $dirName
 * @param bool|true $subdir
 * @return bool
 */
function del_dir_and_file($dirName,$subdir = true)
{
    if(!is_dir($dirName)) return true;
    if ($handle = opendir("$dirName")) {
        while (false !== ($item = readdir($handle))) {
            if ($item != "." && $item != "..") {
                if (is_dir("$dirName/$item")){
                    del_dir_and_file("$dirName/$item", false);
                }
                else
                    @unlink("$dirName/$item");
            }
        }
        closedir($handle);
        if (!$subdir) @rmdir($dirName);
    }
}

function error_code($code, $mini = false)
{
    $result = [
        'status' => false,
        'data'   => $code,
        'msg'    => config('error.10000')
    ];
    if (config('?error.' . $code)) {
        $msg = config('error.' . $code);
        $result['msg'] = $msg;
    }
    if ($mini) {
        return $result['msg'];
    } else {
        return $result;
    }
}

function redirect_url($url = "")
{
    if (cookie('?redirect_url')) {
        $str = cookie('redirect_url');
        cookie('redirect_url', null);
    } else {
        if ($url) {
            $str = $url;
        } else {
            $str = '/';
        }

    }
    return $str;
}




function agent() {
    if(!isset($_SERVER['HTTP_USER_AGENT'])){return 'pc';}
    $agent = $_SERVER ['HTTP_USER_AGENT'];
    if(strpos ( $agent, "android" ) || strpos ( $agent, "Android" )){
        return 'android';
    }
    if(strpos($agent, 'iphone') || strpos($agent, 'ipad')){
        return 'ios';
    }
    if(strpos($agent, 'iPhone') || strpos($agent, 'iPad')){
        return 'ios';
    }
    return 'pc';
}


function is_weixin() {
    if(!isset($_SERVER['HTTP_USER_AGENT'])){return 'pc';}
    $agent = $_SERVER ['HTTP_USER_AGENT'];
    if(strpos ( $agent, "icroMessenger" ) || strpos ( $agent, "MicroMessenger" ) || strpos ( $agent, "WindowsWechat" ) ){
        return true;
    }
    return false;
}



function priceFormat($price) {
    return number_format($price,2,'.','');
}
function numFormat($num,$len=2){
    return number_format($num,$len,'.','');
}




function password_encode($pass){
    return sha1('shop'.md5($pass));
}
//把int类型数据格式化
function format_intdate($timestamp,$rule='Y-m-d H:i:s',$empty=''){
    if(empty($timestamp)){
        return $empty;
    }else{
        return date($rule,$timestamp);
    }
}


function encryptShow($str,$start,$length) {
    $end = $start - 1 + $length;
    $array = str_split($str);
    foreach ($array as $k => $v) {
        if ($k >= $start-1 && $k < $end) {
            $array[$k] = '*';
        }
    }
    return implode('',$array);
}


function suc($data,$msg=''){
    return ['code'=>0,'msg'=>$msg,'data'=>$data];
}
function msuc($msg=''){
    return ['code'=>0,'msg'=>$msg,'data'=>[]];
}
function merr($msg=''){
    return ['code'=>1,'msg'=>$msg,'data'=>[]];
}
function psuc($data,$count,$msg=''){
    if(empty($data)){
        $msg = '暂无数据';
    }
    return ['code'=>0,'count'=>$count,'msg'=>$msg,'data'=>$data];
}
function gsuc($paginate,$msg=''){
    $arr = $paginate->toArray();
    return psuc($arr['data'],$arr['total'],$msg);
}
function sore($beal,$suc_msg='操作成功',$err_msg='操作失败'){
    return $beal ? msuc($suc_msg) : merr($err_msg);
}



